package com.lsh.payment.api.service.exception;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.lsh.payment.api.model.basevo.BaseResponse;
import org.apache.log4j.Logger;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import java.util.Date;

/**
 * Project Name: lsh-payment
 * Author: peter
 * 北京链商电子商务有限公司
 * Desc: 业务异常mapper
 * Package Name:
 * Time: 2018-06-05 下午2:51
 */
public class GlobalExceptionMapper implements ExceptionMapper<Throwable> {

    private final Logger logger = Logger.getLogger(this.getClass());

    public Response toResponse(Throwable throwable) {
        logger.error("globalExceptionMapper filter " + throwable.getMessage(), throwable);
        StringBuffer msg = new StringBuffer();
        msg.append(throwable.getMessage());
        msg.append(" case by :");
        msg.append(throwable.getCause() != null ? throwable.getCause().getMessage() : "");

        Object res = new BaseResponse(1001001, msg.toString(), new Date());
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                .entity(res).type(ContentType.APPLICATION_JSON_UTF_8)
                .build();
    }

}
